# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=hexchat
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.16.1
pkgrel=5
pkgdesc='A popular and easy to use graphical IRC (chat) client (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://hexchat.github.io/'
license=('GPL')
depends=("${MINGW_PACKAGE_PREFIX}-dbus-glib"
         "${MINGW_PACKAGE_PREFIX}-desktop-file-utils"
         "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtk2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pango")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-iso-codes"
             "${MINGW_PACKAGE_PREFIX}-luajit"
             #"${MINGW_PACKAGE_PREFIX}-perl"
             "${MINGW_PACKAGE_PREFIX}-python-cffi")
optdepends=("${MINGW_PACKAGE_PREFIX}-enchant: Spell check"
            "${MINGW_PACKAGE_PREFIX}-iso-codes: Display language names instead of codes"
            "${MINGW_PACKAGE_PREFIX}-luajit: Lua plugin"
            #"${MINGW_PACKAGE_PREFIX}-perl: Perl plugin"
            "${MINGW_PACKAGE_PREFIX}-python-cffi: Python plugin")
source=("https://dl.hexchat.net/hexchat/hexchat-${pkgver}.tar.xz"
        0001-meson-add-winpthread.patch
        0002-fix-plugin-search.patch)
sha256sums=('a7e497da71df419daf9855582d51c723a2611de25d5e97b5efc33fe78da5c4c7'
            '39dc5fe131bcd8575e0d3fd274fa4bf3b59e1d9873ad629ff176028619edf268'
            '02d87882b514628acdb05ffe21a716eb74a97dc28eae517f88a1db3c62f0c19f')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-meson-add-winpthread.patch"
  patch -p1 -i "${srcdir}/0002-fix-plugin-search.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  #
  # Options are from upstream/.github/workflows/msys-build.yml
  #
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe \
    --prefix="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    --wrap-mode=nodownload \
    -Dtext-frontend=true \
    -Dwith-upd=false \
    -Dwith-perl=false \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson.exe test || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
